package game.game;

import com.crunch.core.Actor;
import com.crunch.core.Renderable;
import com.crunch.graphics.gpu.GpuState;
import com.crunch.graphics.gpu.GpuTexture;
import com.crunch.math.Vector2f;
import com.crunch.physics.Capsule;
import com.crunch.physics.DynamicPhysicsEntity;

/**
 * Created by njanetos on 2/24/14.
 *
 * Enemy is a test class for me to learn about the engine. He walks back and forth.
 *
 */
public class Enemy extends Actor implements DynamicPhysicsEntity.EventHandler, Renderable {

    // why were these placed at the end of the player class?
    private GpuTexture texture;
    private DynamicPhysicsEntity physicsEntity;

    // -1: left, 1: right
    private int direction = 1;
    private float distance = 0.0f;

    // what does this do and why
    public Enemy() {
        super(0);
    }

    @Override
    public void onCreate() {
        RoomState roomState = (RoomState) getGameState();

        getEngine().window.renderer.register(this);
        texture = roomState.getCoreState().resourceManager.getTexture("player");
        texture.acquire();

        DynamicPhysicsEntity.Description desc = new DynamicPhysicsEntity.Description();
        desc.shape = new Capsule(1.0f, 0.5f);
        desc.rotation = 0.0f;
        desc.category = 0;
        desc.initialPosition = new Vector2f(10f, 5.0f);
        desc.initialVelocity = new Vector2f();
        desc.mass = 1.0f;
        desc.sticksToFloor = true;
        desc.floorNormal = new Vector2f(0.0f, 1.0f);
        desc.maxFloorAngle = (float) Math.PI / 4.0f;
        desc.sticksToFloorWhenPushed = true;
        desc.repelAcceleration = 1.0f;
        desc.data = this;
        desc.eventHandler = this;
        physicsEntity = roomState.getPhysicsSimulation().addDynamicEntity(desc);
    }

    @Override
    public void onDestroy() {
        physicsEntity.remove();

        getEngine().window.renderer.unregister(this);
        texture.release();
    }

    public DynamicPhysicsEntity getPhysicsEntity() {
        return physicsEntity;
    }

    @Override
    public void render(GpuState gpuState) {
        //
    }

    @Override
    public int getDepth() {
        return 0;
    }

    @Override
    public void onPreStep(DynamicPhysicsEntity entity) {

    }

    @Override
    public void onPostStep(DynamicPhysicsEntity entity) {

    }

    @Override
    public void onProcessInput(DynamicPhysicsEntity entity, float dt) {
        distance += 20.0f*dt;
        if (distance > 50.0f) {
            distance = 0.0f;
            direction *= -1;
        }
        entity.setVelocity(new Vector2f(direction*dt*20.0f, 0));
    }
}
